package com.ruoyi.web.controller.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.OrderConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.PlatfromEnums;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.system.domain.BsOrderDetails;
import com.ruoyi.system.domain.BsOrderInfo;
import com.ruoyi.system.domain.BsPingjia;
import com.ruoyi.system.domain.vo.UserManagerVo;
import com.ruoyi.system.domain.vo.UserOrderVo;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.ruoyi.web.controller.biz
 * @Author: HanWL
 * @CreateTime: 2023-10-27 10:48
 * @Version: 1.0
 **/

@RestController
@Api(tags = "用户管理接口")
@RequestMapping("/bs/user")
public class BsUserController extends BaseController {

    @Resource
    private ISysUserService userService;
    @Resource
    private BsPingjiaService bsPingjiaService;
    @Resource
    private BsOrderInfoService bsOrderInfoService;
    @Resource
    private BsOrderDetailsService bsOrderDetailsService;
    @Resource
    private BsMenpiaoNumService bsMenpiaoNumService;

    @ApiOperation("用户管理分页接口")
    @GetMapping("page")
    public TableDataInfo<?> page() {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getPlatform, PlatfromEnums.Visitor.name());
        startPage();
        List<SysUser> list = userService.list(lqw);
        TableDataInfo dataTable = getDataTable(list);
        List<UserManagerVo> userManagerVos = ConvertUtil.sourceToTarget(list, UserManagerVo.class);
        userManagerVos.forEach(x -> {
            x.setPinglunCount(bsPingjiaService.count(new LambdaQueryWrapper<BsPingjia>().eq(BsPingjia::getUserId, x.getUserId())));
            x.setOrderCount(bsOrderInfoService.count(new LambdaQueryWrapper<BsOrderInfo>().eq(BsOrderInfo::getUserId, x.getUserId()).eq(BsOrderInfo::getOrderUseStatus, OrderConstants.USE_STATUS_WAIT)));
            x.setEnterCount(bsOrderInfoService.count(new LambdaQueryWrapper<BsOrderInfo>().eq(BsOrderInfo::getUserId, x.getUserId()).eq(BsOrderInfo::getOrderUseStatus, OrderConstants.USE_STATUS_ALREADY)));
            x.setGonglueCount(999);
        });
        dataTable.setRows(userManagerVos);
        return dataTable;
    }

    @ApiOperation("用户管理-购票信息统计")
    @GetMapping("pageOrder")
    public TableDataInfo<?> pageOrder() {
        LambdaQueryWrapper<BsOrderInfo> lqw = new LambdaQueryWrapper<>();
        lqw.ne(BsOrderInfo::getOrderUseStatus, OrderConstants.USE_STATUS_CANCEL);
        lqw.ne(BsOrderInfo::getOrderUseStatus, OrderConstants.USER_STATUS_STOP);
        lqw.orderByDesc(BsOrderInfo::getCreateTime);
        startPage();
        List<BsOrderInfo> list = bsOrderInfoService.list(lqw);
        TableDataInfo dataTable = getDataTable(list);
        List<UserOrderVo> resultList = new ArrayList<>();
        for (BsOrderInfo bsOrderInfo : list) {
            UserOrderVo userOrderVo = new UserOrderVo();
            SysUser user = userService.getById(bsOrderInfo.getUserId());
            userOrderVo.setUserId(user.getUserId());
            userOrderVo.setPhonenumber(user.getPhonenumber());
            userOrderVo.setNickName(user.getNickName());

            StringBuilder sb = new StringBuilder();
            List<BsOrderDetails> orderDetailsList = bsOrderDetailsService.list(new LambdaQueryWrapper<BsOrderDetails>().eq(BsOrderDetails::getOrderId, bsOrderInfo.getOrderId()));
            for (BsOrderDetails bsOrderDetails : orderDetailsList) {
                sb.append(bsMenpiaoNumService.getCategoryByNumId(bsOrderDetails.getMenpiaoNumId()).getMenpiaoName());
                sb.append(",");
            }
            userOrderVo.setMenpiaoCategory(sb.toString());
            userOrderVo.setOrderCreateTime(bsOrderInfo.getCreateTime());
            userOrderVo.setOrderUserStatus(bsOrderInfo.getOrderUseStatus());
            userOrderVo.setOrderEnterTime(bsOrderInfo.getOrderEnterTime());
            userOrderVo.setOrderCount(bsOrderInfoService.count(new LambdaQueryWrapper<BsOrderInfo>().eq(BsOrderInfo::getOrderPayStatus,OrderConstants.PAY_STATUS_YES)));
            userOrderVo.setEnterCount(bsOrderInfoService.count(new LambdaQueryWrapper<BsOrderInfo>().eq(BsOrderInfo::getOrderUseStatus,OrderConstants.USE_STATUS_ALREADY)));
            resultList.add(userOrderVo);
        }
        dataTable.setRows(resultList);
        return dataTable;
    }


}
